#pragma once
#include <sys/epoll.h>
#include <iostream>
#include "log.hpp"
#include "nocopy.hpp"

extern Log lg;

class epoller : public nocopy
{
public:
    epoller()
    {
        _epfd = epoll_create(128);
        if (_epfd == -1)
        {
            lg(Error, "epoller creat fail, errno:%d, errstr:%s", errno, strerror(errno));
            exit(2);
        }
    }

    ~epoller()
    {
        if (_epfd >= 0)
            close(_epfd);
    }

    int epollFileDel(const int fd) { return epoll_ctl(_epfd, EPOLL_CTL_DEL, fd, nullptr); }

    int epollFileUpdate(const int op, const int fd, const uint32_t events)
    {
        struct epoll_event newEvent;
        newEvent.data.fd = fd;
        newEvent.events = events;
        return epoll_ctl(_epfd, op, fd, &newEvent);
    }

    int epollWait(struct epoll_event *events, const int maxEvent, const int timeout)
    {
        return epoll_wait(_epfd, events, maxEvent, timeout);
    }

private:
    int _epfd;
};